package cn.tedu._05mvcboot02.common.exception;

import cn.tedu._05mvcboot02.common.response.JsonResult;
import cn.tedu._05mvcboot02.common.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public JsonResult toDoIllegalArgumentException(IllegalArgumentException ex){
        String data = ex.getMessage();

        log.error("IllegalArgumentException = "+ data);

        return new JsonResult(StatusCode.OPERATION_FAILED,data);
    }

    @ExceptionHandler
    public JsonResult toDoRuntimeException(RuntimeException ex){
        String data = ex.getMessage();

        log.error("RuntimeException = " + data);

        return new JsonResult(StatusCode.OPERATION_FAILED,data);
    }

    @ExceptionHandler
    public JsonResult toThrowable(Throwable ex){
        String data = ex.getMessage();

        log.error("Throwable = "+data);

        return new JsonResult(StatusCode.OPERATION_FAILED,data);
    }
}
